package cn.edu.gzhu.workOrderManagement.intercepter;

import cn.edu.gzhu.workOrderManagement.constants.MessageConstant;
import cn.edu.gzhu.workOrderManagement.enumeration.UserAuthorityEnum;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderListDto;
import cn.edu.gzhu.workOrderManagement.pojo.dto.workOrder.WorkOrderUpdateDto;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderDetailVo;
import cn.edu.gzhu.workOrderManagement.pojo.vo.WorkOrderVo;
import cn.edu.gzhu.workOrderManagement.service.WorkOrderService;
import cn.edu.gzhu.workOrderManagement.util.ResponseUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class WorkOrderUpdateInterceptor implements HandlerInterceptor {
    @Autowired
    WorkOrderService workOrderService;
    //工单的当前处理者才可以更新工单
    //只有主管可以转单
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
       //获取Dto
        WorkOrderUpdateDto workOrderUpdateDto = JSONUtil.toBean(body, WorkOrderUpdateDto.class);
        if (workOrderUpdateDto == null||workOrderUpdateDto.getUuid()==null) {
            ResponseUtil.failResponse(response, MessageConstant.PARAMETER_ERROR);
            log.info("更新工单权限校验失败");
            return false;
        }
        //获取操作人信息
        Claims claims = (Claims) request.getAttribute("claims");
        String username = claims.get("username").toString();
        UserAuthorityEnum authority = UserAuthorityEnum.value2Object(claims.get("authority").toString());

        //如果不是主管则不能转单
        if (workOrderUpdateDto.getNeedsReassignment()&& !UserAuthorityEnum.BRANCH_SUPERVISOR.equals(authority)){
            ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
            log.info(authority.getRole(),"{}更新工单权限校验失败");
            return false;
        }

        //获取工单信息，不是处理者不能操作
        IPage<WorkOrderVo> workOrderDetailVoIPage = workOrderService.listWorkOrder(WorkOrderListDto.builder().uuid(workOrderUpdateDto.getUuid()).needDetails(true).pageSize(10).startPage(1).build());
        List<WorkOrderVo> records = workOrderDetailVoIPage.getRecords();
        if (records.isEmpty()) {
            ResponseUtil.failResponse(response, MessageConstant.PARAMETER_ERROR);
            log.info(authority.getRole(),"{}更新工单权限校验失败");
            return false;
        }
        WorkOrderDetailVo workOrderDetailVo = (WorkOrderDetailVo) records.get(0);
        if (!workOrderDetailVo.getTargetUser().equals(username)) {
            ResponseUtil.failResponse(response, MessageConstant.PERMISSION_DENIED);
            log.info(authority.getRole(),"{}更新工单权限校验失败");
            return false;
        }
        return true;

    }

}
